var organizationOrganizationRole_tree;
var organizationItemAndItemProductionLineAndItemProcess_tree;
var organizationOrganizationRole_tree_setting = {
		   check:{
			   enable:true,
			   chkboxType:{"Y":"s","N":"s"},
			   autoCheckTrigger:true
		   },
		   data:{
			   simpleData:{
				   enable:true
			   }
		   },
		   callback:{
			   onCheck:function(){
				   if(organizationItemAndItemProductionLineAndItemProcess_tree == null)
					   return;
					organizationItemAndItemProductionLineAndItemProcess_tree.checkAllNodes(false);
					var organizationOrganizationRole_checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();
					if(organizationOrganizationRole_checkedNodes.length==0)
						return;
					var organizationOrganizationRole_ids = [];
					$.each(organizationOrganizationRole_checkedNodes,function(i,field){
						organizationOrganizationRole_ids.push(field.id);
					});
					$.ajax({
				    	  type: 'POST',
				    	  url: '/getOrganizationItemProcessByOrganizationRole',
				    	  async: false,
				    	  data: {
				    		  "organizationRoleIds": organizationOrganizationRole_ids.join(",")
				    		  },
				    	  success: function(ip_data){
				    		  $.each(ip_data,function(i,itemProcess){
				    			  var item_node = organizationItemAndItemProductionLineAndItemProcess_tree.getNodeByParam("id",itemProcess.itemProductionLine.item.id,null);
				    			  var itemProductionLine_node = organizationItemAndItemProductionLineAndItemProcess_tree.getNodeByParam("id",itemProcess.itemProductionLine.id,item_node);
				    			  var ip_node = organizationItemAndItemProductionLineAndItemProcess_tree.getNodeByParam("id",itemProcess.id,itemProductionLine_node);
				    			  ip_node.checked = true;
				    			  organizationItemAndItemProductionLineAndItemProcess_tree.updateNode(ip_node,true);
				    		  });
				    	  }
				    });
					
				}
		   }
}
var organizationItemAndItemProductionLineAndItemProcess_tree_setting = {
		 check:{
			   enable:true,
			   chkboxType:{"Y":"ps","N":"ps"},
			   autoCheckTrigger:true
		   },
		   data:{
			   simpleData:{
				   pIdKey:"pid",
				   enable:true
			   }
		   },
		   callback:{
			   onCheck:function(){
				}
		   }
}
var organizationItemAndItemProductionLineAndItemProcess_tree_init = function(){
	if(organizationItemAndItemProductionLineAndItemProcess_tree != null)
		organizationItemAndItemProductionLineAndItemProcess_tree.destroy();
	$.ajax({
  	  type: 'POST',
  	  url: '/getAllOrganizationItem',
  	  async: false,
  	  success: function(data){
  		 var organizationItemAndItemProductionLineAndItemProcessData = [];
		  $.each(data,function(index,item){
			  organizationItemAndItemProductionLineAndItemProcessData.push({
				  id:item.id,
				  pid:null,
				  isParent:true,
				  name:item.name
			  });
			  	

				$.ajax({
				    	  type: 'POST',
				    	  async:false,
				    	  url: '/getAllOrganizationItemProductionLineByItem',
				    	  data:{
				    		  "itemId":item.id
				    	  },
				    	  success: function(idata){
				    		  $.each(idata,function(index,itemProductionLine){
				    			  organizationItemAndItemProductionLineAndItemProcessData.push({
				    				  isParent:true,
				    				  id:itemProductionLine.id,
				    				  pid:item.id,
				    				  name:itemProductionLine.name
				    			  });
				    			  $.ajax({
				    		    	  type: 'POST',
				    		    	  async: false,
				    		    	  data:{ itemProductionLineId : itemProductionLine.id},
				    		    	  url: '/getOrganizationItemProcessByItemProductionLine',
				    		    	  success: function(ip_data){
				    		    		  $.each(ip_data,function(index,itemProcess){
				    		    			  organizationItemAndItemProductionLineAndItemProcessData.push({
				    		    				  isParent:false,
				    		    				  id:itemProcess.id,
				    		    				  pid:itemProductionLine.id,
				    		    				  name:itemProcess.name
				    		    			  });
				    		    		  });
				    		    	  }
				    			  });
				    		  });
				    		  
				    		  organizationItemAndItemProductionLineAndItemProcess_tree = $.fn.zTree.init($("#organizationItemAndItemProductionLineAndItemProcess_tree_id"),organizationItemAndItemProductionLineAndItemProcess_tree_setting,organizationItemAndItemProductionLineAndItemProcessData);
				    		  organizationItemAndItemProductionLineAndItemProcess_tree.expandAll(true);
				    	  }
				    });
			  
		  });
  	  }
	});    
	
	
	
}
var organizationOrganizationRole_tree_init = function(){
	if(organizationOrganizationRole_tree != null)
		organizationOrganizationRole_tree.destroy();
	    $.ajax({
	    	  type: 'POST',
	    	  url: '/getAllOrganizationOrganizationRole',
	    	  async: false,
	    	  success: function(data){
	    		  organizationOrganizationRole_tree = $.fn.zTree.init($("#organizationOrganizationRole_tree_id"),organizationOrganizationRole_tree_setting,data);
	    		  organizationOrganizationRole_tree.expandAll(true);
	    	  }
	    });
}
$(function(){ 
	
	organizationOrganizationRole_tree_init();
	organizationItemAndItemProductionLineAndItemProcess_tree_init();
    
    var add_organizationOrganizationRole_btn = $('#add_organizationOrganizationRole_btn_id');
    add_organizationOrganizationRole_btn.click(function(){
    	$('#add_organizationOrganizationRole_Modal').modal("show");
    });
    
    var up_organizationOrganizationRole_btn = $('#up_organizationOrganizationRole_btn_id');
    up_organizationOrganizationRole_btn.click(function(){
    	var checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();//勾选框
		if(checkedNodes.length != 1){
			BootstrapDialog.show({
				type: BootstrapDialog.TYPE_WARNING,
				title: "提示",
				buttons: [ {
		                label: "关闭",
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
				message: "请选择一个角色"
			});
			return;
		}
		var node = checkedNodes[0];
    	$("#up_organizationOrganizationRole_name").val(node.name);
    	$("#up_organizationOrganizationRole_Modal").modal("show");
    });
    
    var del_organizationOrganizationRole_btn = $("#del_organizationOrganizationRole_btn_id");
    
    del_organizationOrganizationRole_btn.click(function(){
    	var checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();//勾选框
		if(checkedNodes.length == 0){
			BootstrapDialog.show({
				type: BootstrapDialog.TYPE_WARNING,
				title: "提示",
				buttons: [ {
		                label: "关闭",
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
				message: "至少选择一个角色"
			});
			return;
		}
		else{
			var ids = [];
			$.each(checkedNodes,function(i,field){
				ids.push(field.id);
			});

			$.ajax({
		    	  type: 'POST',
		    	  url: '/delOrganizationOrganizationRole',
		    	  data: {
		    		  "ids":ids.join(",")
		    		  },
		    	  success: function(data){
		    		  var type;
		    		  if(data.status == 0){
		    			  organizationOrganizationRole_tree_init();
		    			  type = BootstrapDialog.TYPE_SUCCESS;
		    		  }
		    		  else if(data == 1){
		    			  type = BootstrapDialog.TYPE_WARNING;
		    		  }
		    		  else{
		    			  type = BootstrapDialog.TYPE_DANGER;
		    		  }
		    		  BootstrapDialog.show({
		  				type: type,
		  				title: "执行结果",
						buttons: [ {
			                label: '关闭',
			                action: function(dialogItself){
			                    dialogItself.close();
			                }
			            }],
		  				message: data.msg
		  			});
		    	  }
		    	});
		}
    });
    
	
	var add_organizationOrganizationRole_submit_btn = $("#add_organizationOrganizationRole_submit_btn_id");
	add_organizationOrganizationRole_submit_btn.click(function(){

		
		var formArray = getFormDataArray($("#add_organizationOrganizationRole_form_id"));
		
		var formData = eval('({'+formArray.join(',')+'})');

		$.ajax({
	    	  type: 'POST',
	    	  url: '/addOrganizationOrganizationRole',
	    	  data: formData,
	    	  success: function(data){
	    		  $('#add_organizationOrganizationRole_Modal').modal("hide");
	    		  var type;
	    		  if(data.status == 0){
	    			  organizationOrganizationRole_tree_init();
	    			  type = BootstrapDialog.TYPE_SUCCESS;
	    		  }
	    		  else if(data == 1){
	    			  type = BootstrapDialog.TYPE_WARNING;
	    		  }
	    		  else{
	    			  type = BootstrapDialog.TYPE_DANGER;
	    		  }
	    		  BootstrapDialog.show({
	  				type: type,
	  				title: "执行结果",
					buttons: [ {
		                label: '关闭',
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
	  				message: data.msg
	  			});
	    	  }
	    	});
	    	
	});
	
	var up_organizationOrganizationRole_submit_btn = $("#up_organizationOrganizationRole_submit_btn_id");
	up_organizationOrganizationRole_submit_btn.click(function(){
		var checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();//勾选框
		var node = checkedNodes[0];
		var formArray = getFormDataArray($("#up_organizationOrganizationRole_form_id"));
		var id = node.id;
		formArray.push('"id":'+id);
		var formData = eval('({'+formArray.join(',')+'})');
		$.ajax({
	    	  type: 'POST',
	    	  url: '/upOrganizationOrganizationRole',
	    	  data: formData,
	    	  success: function(data){
	    		  $('#up_organizationOrganizationRole_Modal').modal("hide");
	    		  var type;
	    		  if(data.status == 0){
	    			  organizationOrganizationRole_tree_init();
	    			  type = BootstrapDialog.TYPE_SUCCESS;
	    		  }
	    		  else if(data == 1){
	    			  type = BootstrapDialog.TYPE_WARNING;
	    		  }
	    		  else{
	    			  type = BootstrapDialog.TYPE_DANGER;
	    		  }
	    		  BootstrapDialog.show({
	  				type: type,
	  				title: "执行结果",
					buttons: [ {
		                label: '关闭',
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
	  				message: data.msg
	  			});
	    	  }
	    	});
	});
	
	var grant_organizationItemAndItemProductionLineAndItemProcessToOrganizationRole_btn = $("#grant_organizationItemAndItemProductionLineAndItemProcessToOrganizationRole_btn_id");
	grant_organizationItemAndItemProductionLineAndItemProcessToOrganizationRole_btn.click(function(){
    	var organizationOrganizationRole_checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();//勾选框
		if(organizationOrganizationRole_checkedNodes.length == 0){
			BootstrapDialog.show({
				type: BootstrapDialog.TYPE_WARNING,
				title: "提示",
				buttons: [ {
		                label: "关闭",
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
				message: "至少选择一个角色"
			});
			return;
		}
		var organizationOrganizationRole_checkedNodes = organizationOrganizationRole_tree.getCheckedNodes();
		var organizationOrganizationRole_ids = [];
		$.each(organizationOrganizationRole_checkedNodes,function(i,field){
			organizationOrganizationRole_ids.push(field.id);
		});
    	var iiip_checkedNodes = organizationItemAndItemProductionLineAndItemProcess_tree.getCheckedNodes();//勾选框
		var item_ids = [];
    	var itemProductionLine_ids = [];
		var itemProcess_ids = [];
		$.each(iiip_checkedNodes,function(index,node){
			if(node.isParent){
				if(node.pid == null){
					item_ids.push(node.id);
				}
				else{
					itemProductionLine_ids.push(node.id);
				}			
			}
			else{
				itemProcess_ids.push(node.id);
			}
		});

		$.ajax({
	    	  type: 'POST',
	    	  url: '/grantOrganizationItemAndItemProductionLineAndItemProcessToOrganizationRole',
	    	  async: false,
	    	  data: {
	    		  "organizationRoleIds" : organizationOrganizationRole_ids.join(","),
	    		  "itemIds":item_ids.join(","),
	    		  "itemProductionLineIds" : itemProductionLine_ids.join(","),
	    		  "itemProcessIds":itemProcess_ids.join(",")
	    		  },
	    	  success: function(data){
	    		  var type;
	    		  if(data.status == 0){
	    			  type = BootstrapDialog.TYPE_SUCCESS;
	    		  }
	    		  else if(data == 1){
	    			  type = BootstrapDialog.TYPE_WARNING;
	    		  }
	    		  else{
	    			  type = BootstrapDialog.TYPE_DANGER;
	    		  }
	    		  BootstrapDialog.show({
	  				type: type,
	  				title: "执行结果",
					buttons: [ {
		                label: '关闭',
		                action: function(dialogItself){
		                    dialogItself.close();
		                }
		            }],
	  				message: data.msg
	  			});
	    	  }
	    });
    });
});
